package com.shizhanzhe.qdd.home.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.qdd.MainActivity;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.WXLoginActivity;
import com.shizhanzhe.qdd.base.UserBean;
import com.shizhanzhe.qdd.utils.AdPreference;
import com.shizhanzhe.qdd.utils.SharedPreferencesUtils;
import com.shizhanzhe.qdd.utils.Util;

import java.io.File;

/**
 *
 * @author YJ
 * @date 2019/1/4
 */
public class SplashActivity extends Activity {

    private ImageView iv_img;
    private int mtime = 5;
    private TextView tvdao;
    boolean isRun = true;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mtime--;
            tvdao.setText(mtime + "");
            if (mtime == 0&&isRun) {
                SharedPreferencesUtils.setDevice(SplashActivity.this);
                if (UserBean.uid==0){
                    startActivity(new Intent(SplashActivity.this, WXLoginActivity.class));
                }else {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                }

//                isShareprefence();
                isRun = false;
                finish();
            }
        }
    };
    String imgUrl = "";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ImmersionBar.with(this)
                .transparentBar()
                .init();
        UserBean.uid = (int) SharedPreferencesUtils.getParam(this, "uid", 0);
        initData();
        if (SharedPreferencesUtils.getDevice(this) != 0) {
            isFirst();
        }
    }

    private void initData(){

        iv_img = findViewById(R.id.iv_img);
        View tiaoguo = findViewById(R.id.ll_tiaoguo);
        tvdao = findViewById(R.id.tv_dao);
        imgUrl = AdPreference.getInstance().getStartupAdPage().getRelatedUrl();

        if ("".equals(imgUrl)) {
            tiaoguo.setVisibility(View.GONE);
            mtime=3;
        } else {
            mtime=5;
            tiaoguo.setVisibility(View.VISIBLE);
            tiaoguo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    SharedPreferencesUtils.setDevice(SplashActivity.this);
                    if (UserBean.uid==0){
                        startActivity(new Intent(SplashActivity.this, WXLoginActivity.class));
                    }else {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    }
                    isRun = false;
                    finish();

                }
            });
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRun) {
                    try {
                        Thread.sleep(1000); // sleep 1000ms
                        Message message = Message.obtain();
                        message.what = 1;
                        mHandler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
        iv_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!"".equals(imgUrl)) {
                    Uri uri = Uri.parse(imgUrl);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            }
        });
    }
    private void isFirst() {
        String AD_PATH = "";
        AD_PATH = Environment.getExternalStorageDirectory() + "/MFAd/" + Util.getImgName(AdPreference.getInstance().getStartupAdPage().getPicUrl());
        File file = new File(AD_PATH);
        Bitmap bm = BitmapFactory.decodeFile(AD_PATH);

        if (file.exists() && bm != null) {
            iv_img.setScaleType(ImageView.ScaleType.FIT_XY);
            iv_img.setImageBitmap(bm);
        } else {
            iv_img.setScaleType(ImageView.ScaleType.FIT_CENTER);
            Util.deleteFile(AD_PATH);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
